--[[
Name: KuBa-Bleed-1.0
Revision: $Rev: 10004 $
Author(s): Crazydru, 树先生
Website: https://gitee.com/ku-ba
Description: 判断单位可否流血库。
Dependencies: AceLibrary, AceEvent-2.0, SpecialEvents-Aura-2.0 
]]

-- 主要版本
local MAJOR_VERSION = "KuBa-Bleed-1.0"
-- 次要版本
local MINOR_VERSION = "$Revision: 10004 $"

-- 检验AceLibrary
if not AceLibrary then
	error(MAJOR_VERSION .. " requires AceLibrary")
end

-- 检验版本（本库，单实例）
if not AceLibrary:IsNewVersion(MAJOR_VERSION, MINOR_VERSION) then
	return
end

-- 检查依赖库
---@param dependencies table 依赖库名称列表
local function CheckDependency(dependencies)
	for _, value in ipairs(dependencies) do
		if not AceLibrary:HasInstance(value) then 
			error(format("%s requires %s to function properly", MAJOR_VERSION, value))
		end
	end
end

CheckDependency({
	-- 事件
	"AceEvent-2.0",
	-- 光环
	"SpecialEvents-Aura-2.0"
})

-- 判断单位可否流血库
---@class KuBa-Bleed-1.0
local Library = {}

--------------------------------

-- 流血法术
local BLEED_SPELLS = {
	["撕扯"] = true,
	["割裂"] = true,
	["绞喉"] = true,
	["撕裂"] = true,
	["血袭"] = true,
	["扫击"] = true,
}

-- 免疫模式
local RESIST_PATTERN = "你的(.+)施放失败。(.+)对此免疫"

-- 未命中模式
local MISS_PATTERNS = {
	"你的(.+)没有击中(.+)",
	"你的(.+)被(.+)躲闪过去了",
	"你的(.+)被(.+)招架了",
	"你的(.+)被(.+)格挡了",
}

-- 可否流血；更新于：2025-10-14
local canBleeds = {
	-- 卡拉赞上层
	["地狱之怒碎片"] = false,
	["噩梦爬行者"] = false,
	["麦迪文的回响"] = false,
	["荒芜的入侵者"] = false,

	-- 纳克萨玛斯
	["瘟疫战士"] = false,
	["邪恶之斧"] = false,
	["邪恶法杖"] = false,
	["邪恶之剑"] = false,
	["骷髅骏马"] = false,
	["死灵骑士卫兵"] = false,
	["死灵骑士"] = false,
	["纳克萨玛斯之影"] = false,
	["白骨构造体"] = false,
	["纳克萨玛斯之魂"] = false,
	["憎恨吟唱者"] = false,

	-- 熔火之心
	["加尔"] = true,
	["熔核巨人"] = false,
	["暗炉卫士"] = false,
	["暗炉织焰者"] = false,
	["暗炉圣职者"] = false,
	["法师领主索瑞森"] = false,

	-- 卡拉赞下层
	["幻影守卫"] = false,
	["幽灵厨师"] = false,
	["闹鬼铁匠"] = false,
	["幻影仆从"] = false,
	["莫罗斯"] = false,

	-- 斯坦索姆
	["安娜丝塔丽男爵夫人"] = false,
	["埃提耶什"] = false,

	-- 其他
	["幽灵训练师"] = false,
	["受难的上层精灵"] = false,
	["死亡歌手"] = false,
	["恐怖编织者"] = false,
	["哀嚎的死者"] = false,
	["亡鬼幻象"] = false,
	["骷髅刽子手"] = false,
	["恐惧骸骨"] = false,
	["恐怖骸骨"] = false,
	["骷髅巫师"] = false,
	["骷髅战士"] = false,
	["骷髅法师"] = false,
	["骷髅袭击者"] = false,
	["骷髅侍僧"] = false,
	["游荡的骷髅"] = false,
	["骷髅铁匠"] = false,
	["天灾勇士"] = false,
	["魔鳞魔网搜寻者"] = false,
	["不安宁的阴影"] = false,
	["不死的看守者"] = false,
	["哀嚎的鬼怪"] = false,
	["被诅咒的灵魂"] = false,
	["不死的居民"] = false,
	["幽灵工人"] = false,
	["影爪狼人"] = false,
	["影爪暗行者"] = false,
	["魔鳞织法者"] = false,
	["鬼灵训练师"] = false,
	["徘徊的魔法师"] = false,
	["徘徊的占星家"] = false,
	["徘徊的魔术师"] = false,
	["徘徊的工匠"] = false,
	["上层精灵鬼巫"] = false,
	["上层精灵的幻影"] = false,
	["恶魔之心"] = false,
	["战争使者监军"] = false,
	["兵卒"] = false,
	["共鸣水晶"] = false,
	["灌木塑根者"] = true,
	["灌木露水收集者"] = true,
	["长瘤的灌木兽"] = true,
	["曲木撕裂者"] = true,
	["曲木食苔者"] = true,
	["迪塞库斯"] = true,
	["铁木践踏者"] = true,
	["铁木游荡者"] = true,
	["枯萎的保护者"] = true,
	["废墟爬行者"] = true,
	["血瓣花掠夺者"] = true,
	["血瓣花猛击者"] = true,
	["血瓣花捕兽者"] = true,
	["血瓣花鞭笞者"] = true,
	["焦油兽王"] = true,
	["焦油潜伏者"] = true,
	["黑暗法师"] = false,
	["黑衣守卫斥候"] = false,
	["哀嚎的女妖"] = false,
	["尖叫的女妖"] = false,
	["无眼观察者"] = false,
	["鬼魅随从"] = false,
	["艾德雷斯妖灵"] = false,
	["失控的骑士"] = true,
}

-- 检验单位能否流血
---@param unit string 单位
---@return boolean can 可否
function Library:canBleed(unit)
	-- 无效单位
	if not UnitExists(unit) then
		return false
	end

	-- 补充列表
	local name = UnitName(unit)
	if canBleeds[name] == nil then
		local kind = UnitCreatureType(unit)
		canBleeds[name] = kind ~= "机械" and kind ~= "元素生物"
	end

	-- 检验减益
	if canBleeds[name] == false then
		for spell in pairs(BLEED_SPELLS) do
			if UnitHasAura(unit, spell) then
				canBleeds[name] = true
				break
			end
		end
	end
	return canBleeds[name]
end

-- 监听玩家施法的战斗日志信息
---@param arg1 string 战斗日志信息
function Library:CHAT_MSG_SPELL_SELF_DAMAGE(arg1)
	-- 匹配免疫
	local _, _, spell, name = string.find(arg1, RESIST_PATTERN)
	if spell and name then
		if BLEED_SPELLS[spell] and canBleeds[name] == true then
			canBleeds[name] = false
		elseif not spell == "精灵之火（野性）" then
			canBleeds[name] = true
		end
	else
		-- 匹配未击中
		for _, pattern in pairs(MISS_PATTERNS) do
			local _, _, spellName, targetName = string.find(arg1, pattern)
			if spellName and targetName then
				if BLEED_SPELLS[spellName] and canBleeds[targetName] == false then
					canBleeds[targetName] = true
				end
				break
			end
		end
	end
end

--------------------------------

-- 库激活
---@param self table 库自身对象
---@param oldLib table 旧版库对象
---@param oldDeactivate function 旧版库停用函数
local function activate(self, oldLib, oldDeactivate)
	-- 新版本使用
	Library = self

	-- 旧版本释放
	if oldLib then
		-- ...
	end

	-- 新版本初始化
	-- ...

	-- 旧版本停用
	if oldDeactivate then
		oldDeactivate(oldLib)
	end
end

-- 外部库加载
---@param self table 库自身对象
---@param major string 外部库主版本
---@param instance table 外部库实例
local function external(self, major, instance)
	if major == "AceEvent-2.0" then
		-- 混入事件
		instance:embed(self)
		-- 注册事件
		self:RegisterEvent("CHAT_MSG_SPELL_SELF_DAMAGE")
	end
end

-- 最终注册库
AceLibrary:Register(Library, MAJOR_VERSION, MINOR_VERSION, activate, nil, external)
---@diagnostic disable-next-line
Library = nil
